//
//  NoticeDetailViewController.m
//  XTXOneToOne
//
//  Created by Canny on 2018/11/9.
//  Copyright © 2018年 WK. All rights reserved.
//

#import "NoticeDetailViewController.h"
#import "NotioceDetailModel.h"
@interface NoticeDetailViewController ()

@property (nonatomic,strong)NotioceDetailModel * DetailModel;
@property (nonatomic,strong)UILabel * lab_title;
@property (nonatomic,strong)UITextView * textView_content;

@end

@implementation NoticeDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    [self setProperty];
//    [self cereateUI];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBarHidden=YES;
    
    [self Http_notice_find];
}

-(void)setProperty{
    
    
    self.view.backgroundColor=RGB(255, 255, 255);
    
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBarHidden=YES;
    
    self.baseVcTitle = @"公告详情";
    
    
}

/*
 * 接口地址
 
 请求方式：post
 请求接口：notice/find
 需要登录：是
 2、接口请求参数
 
 参数名    参数类型    是否必须    含义    备注
 notice_id    整型    是    公告id

 */
#pragma mark-公告详情
-(void)Http_notice_find{
    
    
    NSString * URL = [NSString stringWithFormat:@"%@notice/find",DEFAULTSEVERBASEURL];
    NSDictionary * Dic = @{
                           @"notice_id":_NLModel.ID
                           };
    WS(weakSelf);
    [HUD showProgress:@"" inView:self.bgView];
    
    [MOLoadHttpsData PostHttpDataWithUrlStr:URL Dic:Dic SuccessBlock:^(id  _Nonnull responseObject) {
        [HUD dismissInView:weakSelf.bgView];
        
        NSDictionary * dic = responseObject;
        NSString * msg = [NSString stringWithFormat:@"%@",dic[@"msg"]];
        NSString * code = [NSString stringWithFormat:@"%@",dic[@"code"]];
        if ([code isEqualToString:@"0"]) {
            
            NSDictionary * dicc = dic[@"data"];
            weakSelf.DetailModel = [NotioceDetailModel mj_objectWithKeyValues:dicc];
            [weakSelf cereateUI];
        }else{
            
            [HUD showErrorMessage:msg];
            
        }
    } FailureBlock:^(id  _Nonnull error) {
        [HUD dismissInView:weakSelf.bgView];
    }];

}

-(void)cereateUI{
    
    _lab_title = [[UILabel alloc]init];
    [self setLab:_lab_title withColor:RGB(40,40,40) andFont:25 andText:self.DetailModel.title andTextAlignment:NSTextAlignmentCenter];
    _lab_title.frame=CGRectMake(0, 0, G_SCREENWIDTH, 50);
    [self.bgView addSubview:_lab_title];

    
    
    UIView * view = [[UIView alloc]init];
    view.backgroundColor=[UIColor clearColor];
    [self.bgView addSubview:view];
    view.frame=CGRectMake(15, 50, G_SCREENWIDTH-30, 1);
    
    CAShapeLayer *border = [CAShapeLayer layer];
    //虚线的颜色
    border.strokeColor = [UIColor grayColor].CGColor;
    //填充的颜色
    border.fillColor = [UIColor clearColor].CGColor;
    //设置路径
    border.path = [UIBezierPath bezierPathWithRect:view.bounds].CGPath;
    border.frame = view.bounds;
    //虚线的宽度
    border.lineWidth = 0.3f;
    //设置线条的样式
    //    border.lineCap = @"square";
    //虚线的间隔
    border.lineDashPattern = @[@4, @2];
    [view.layer addSublayer:border];
    
    
    UITextView * textView_content = [[UITextView alloc]init];
    textView_content.userInteractionEnabled=NO;
    [self.bgView addSubview:textView_content];
    textView_content.textColor=RGB(100, 100, 100);
    textView_content.font=[UIFont systemFontOfSize:14];
    [textView_content mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.bgView.mas_right).offset(-15);
        make.left.equalTo(self.bgView.mas_left).offset(15);
        make.bottom.equalTo(self.bgView.mas_bottom).offset(-10);
        make.top.equalTo(view.mas_bottom).offset(10);
    }];
    
    NSString * content = [NSString stringWithFormat:@"%@\n\n%@",[self filterHTML:self.DetailModel.content],[self GetKongGe]];
    textView_content.text = content;
    
}

-(NSString *)filterHTML:(NSString *)html
{
    NSScanner * scanner = [NSScanner scannerWithString:html];
    NSString * text = nil;
    while([scanner isAtEnd]==NO)
    {
        [scanner scanUpToString:@"<" intoString:nil];
        [scanner scanUpToString:@">" intoString:&text];
        html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@""];
    }
    return html;
}

-(NSString *)GetKongGe{
    CGFloat W = G_SCREENWIDTH-30-20,length = 0;
    NSString * time = [NSString stringWithFormat:@"%@",self.DetailModel.create_time];
    
    while (1) {
        
        if (length<W) {
            time = [NSString stringWithFormat:@"  %@",time];
            length=[self getAttributeSizeWithText:time fontSize:14].width;
        }else{
            return time;
        }
    }
}

-(CGSize) getAttributeSizeWithText:(NSString *)text fontSize:(int)fontSize
{
    CGSize size=[text sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]}];
    size=[text sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]}];
    
    return size;
}

-(void)setLab:(UILabel *)lab withColor:(UIColor *)color andFont:(CGFloat)font andText:(NSString *)text andTextAlignment:(NSTextAlignment)textAlignment{
    lab.text=text;
    lab.textColor=color;
    lab.font=[UIFont systemFontOfSize:font];
    lab.textAlignment=textAlignment;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
